/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifndef AFC_XMLPARSER_H
#define AFC_XMLPARSER_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#include "../afc.h"

//includes from libxml
#include <libxml/parser.h>
#include <libxml/parserInternals.h>


/* AFC afc_xmlparser Magic Number */
#define AFC_XMLPARSER_MAGIC ( 'X' << 24 | 'M' << 16 | 'L' << 8 | 'P' )

/* AFC afc_xmlparser Base value for constants */
#define AFC_XMLPARSER_BASE 0xB000


/* Errors for afc_xmlparser */

#define AFC_XMLPARSER_ERR_NO_ERROR          0                       /* No error occurred                       */
#define AFC_XMLPARSER_ERR_NO_MEMORY         AFC_XMLPARSER_BASE + 1  /* No memory for required operation        */
#define AFC_XMLPARSER_ERR_NULL_POINTER      AFC_XMLPARSER_BASE + 2  /* Null pointer passed as argument         */
#define AFC_XMLPARSER_ERR_INVALID_POINTER   AFC_XMLPARSER_BASE + 3  /* Pointer passed is not of the right kind */
#define AFC_XMLPARSER_ERR_NO_CTXT	          AFC_XMLPARSER_BASE + 4  /* no sax context */
#define AFC_XMLPARSER_ERR_BADFORMED         AFC_XMLPARSER_BASE + 5  /* xml document is not well formed */
#define AFC_XMLPARSER_ERR_INVALID_STRING		AFC_XMLPARSER_BASE + 6  /* string parameter is NULL or empty */
#define AFC_XMLPARSER_ERR_UNKNOWN_CALLBACK	AFC_XMLPARSER_BASE + 7  /* callback not found in internal list */


/* special callback types */
#define AFC_XMLPARSER_CBTYPE_TAG_UNKNOWN	1
#define AFC_XMLPARSER_CBTYPE_START_DOC		2
#define AFC_XMLPARSER_CBTYPE_END_DOC			3
#define AFC_XMLPARSER_CBTYPE_CHARACTERS		4
#define AFC_XMLPARSER_CBTYPE_WARNING			5
#define AFC_XMLPARSER_CBTYPE_ERROR				6
#define AFC_XMLPARSER_CBTYPE_FATAL_ERROR	7


/* forward declarations */
struct afc_xmlparser;

typedef struct afc_xmlparser XmlParser;


/* structures declarations */

struct afc_xmlparser_state {
	XmlParser * xmlparser;
	void * userdata;
};

typedef struct afc_xmlparser_state XmlParserState;
typedef void ( * BeginCallbackFunc ) ( void * , Dictionary * );
typedef void ( * EndCallbackFunc ) ( void * xmlpstate );
typedef void ( * TagUnknownCallbackFunc ) ( void * , const xmlChar * , Dictionary * );


struct afc_xmlparser_callback_tuple {
	BeginCallbackFunc beginCallback;
	EndCallbackFunc endCallback;
};

typedef struct afc_xmlparser_callback_tuple XmlParserCallbackTuple;


struct afc_xmlparser 
{
	unsigned long magic;
	xmlSAXHandlerPtr sax_handler;
	xmlParserCtxtPtr sax_context;
	XmlParserState * state;
	Dictionary * callBacks;
	Dictionary * entities;
	Dictionary * element_attrs;
	TagUnknownCallbackFunc tagUnknown;
	void ( * startDocument ) ( void * );
	void ( * endDocument ) ( void * );
	void ( * characters ) ( void *, const xmlChar *, int );
	void ( * warning ) ( void *, const char * );
	void ( * error ) ( void *, const char * );
	void ( * fatalError ) ( void *, const char * );
};


/* XmlParser functions */

XmlParser * afc_xmlparser_new ();
int afc_xmlparser_delete ( XmlParser * ) ;
int afc_xmlparser_clear ( XmlParser * ) ;
int afc_xmlparser_parse_file ( XmlParser *, char *, void * );
int afc_xmlparser_add_callback ( XmlParser *, char *, BeginCallbackFunc, EndCallbackFunc );
int afc_xmlparser_del_callback ( XmlParser *, char * );
int afc_xmlparser_set_callback ( XmlParser *, int, void * );
int afc_xmlparser_entity_set ( XmlParser *, char *, int, char *, char *, char * );

int afc_xmlparser_buffer_append ( char **, const xmlChar *, int len );
int afc_xmlparser_buffer_clear ( char ** );
int afc_xmlparser_buffer_delete ( char ** );

/* internal functions: do not call! */

void afc_xmlparser_internal_startElement ( void *, xmlChar *, const xmlChar ** );
void afc_xmlparser_internal_endElement ( void *, xmlChar * );
void afc_xmlparser_internal_startDocument ( void * );
void afc_xmlparser_internal_endDocument ( void * );
void afc_xmlparser_internal_characters ( void *, const xmlChar *, int );
void afc_xmlparser_internal_entity_del ( xmlEntity * );
xmlEntityPtr afc_xmlparser_internal_entity_handler ( void *, const xmlChar * );
void afc_xmlparser_internal_warning ( void *, const char *);
void afc_xmlparser_internal_error ( void *, const char *);
void afc_xmlparser_internal_fatal_error ( void *, const char *);
//void afc_xmlparser_internal_reference ( void *, const xmlChar * );
//void afc_xmlparser_internal_entityDecl ( void *, const xmlChar *, int, const xmlChar *, const xmlChar *, xmlChar * );
int afc_xmlparser_internal_attributes_clear ( XmlParser * );
int afc_xmlparser_internal_attributes_parse ( XmlParser *, const xmlChar ** );

#endif
